# coding=UTF-8
__author__ = 'Administrator'
from py2neo import *


# 'http://localhost:7474',username='neo4j',password='test'
class getInfoFromKnowledgeGraph():
    def __init__(self):
        self.team_dict = {'火箭': '休斯顿火箭',
                          '国王': '萨克拉门托国王',
                          '雷霆': '俄克拉荷马雷霆',
                          '掘金': '丹佛掘金',
                          '湖人': '洛杉矶湖人',
                          '森林狼': '明尼苏达森林狼',
                          '骑士': '克里夫兰骑士',
                          '活塞': '底特律活塞',
                          '爵士': '犹他爵士',
                          '黄蜂': '夏洛特黄蜂',
                          '快船': '洛杉矶快船',
                          '奇才': '华盛顿奇才',
                          '开拓者': '波特兰开拓者',
                          '老鹰': '亚特兰大老鹰',
                          '热火': '迈阿密热火',
                          '太阳': '菲尼克斯太阳',
                          '凯尔特人': '波士顿凯尔特人',
                          '公牛': '芝加哥公牛',
                          '雄鹿': '密尔沃基雄鹿',
                          '勇士': '金州勇士',
                          '尼克斯': '纽约尼克斯',
                          '篮网': '布鲁克林篮网',
                          '猛龙': '多伦多猛龙',
                          '步行者': '印第安纳步行者',
                          '76人': '费城76人',
                          '魔术': '奥兰多魔术',
                          '鹈鹕': '新奥尔良鹈鹕',
                          '马刺': '圣安东尼奥马刺',
                          '灰熊': '孟菲斯灰熊',
                          '独行侠': '达拉斯独行侠'

                          }
        self.graph = Graph('http://localhost:7474', username='neo4j', password='123456')
        self.matcher = NodeMatcher(self.graph)

    def getPartition(self, team):
        # print(team)
        query = "_.球队名称='" + self.team_dict[team] + "'"
        node = self.matcher.match('NBA球队').where(query.encode('utf-8').decode('utf-8')).first()  ##貌似修改正确，中文乱码引起的错误
        # node = self.matcher.match('NBA球队').where(query.decode('utf-8')).first()  ##lynn 这里有问题
        # partition = node.get(u'分区')  ##nodematch has no get lynn 分区改为半区
        partition = node.get(u'半区')
        return partition

    def getLastGame(self, team_a, team_b):
        # match (t1:NBA球队{球队名称:'洛杉矶湖人'})<-[r1:比赛球队]-(g1:比赛)-[r2:比赛球队]->(t2:NBA球队{球队名称:'休斯顿火箭'})  return t1,g1,t2 order by g1.时间 desc limit 1
        query = "match (t1:NBA球队{球队名称:'" + self.team_dict[
            team_a] + "'})<-[r1:比赛球队]-(g1:比赛)-[r2:比赛球队]->(t2:NBA球队{球队名称:'" + self.team_dict[
                    team_b] + "'}) return g1 order by g1.时间 desc limit 1"
        score = self.graph.run(query.encode('utf-8').decode('utf-8')).data()[0]['g1'].get(u'比分')
        id = self.graph.run(query.encode('utf-8').decode('utf-8')).data()[0]['g1'].get('id')
        # print(id)
        score_a = score.split('-')[0]
        score_b = score.split('-')[1]
        query2 = "match (t1:NBA球队{球队名称:'" + self.team_dict[team_a] + "'})-[r1:主场作战]->(g1:比赛{id:'" + id + "'}) return r1"
        # query2 = "match (t1:NBA球队{球队名称:'" + self.team_dict[team_a] + "'})-[r1:主场作战]->(g1:比赛{id:'" + id.encode(
        #     'utf-8') + "'}) return r1"
        data = self.graph.run(query2.encode('utf-8').decode('utf-8')).data()
        if len(data) == 0:
            tag = 0
        else:
            tag = 1
        return int(score_a), int(score_b), tag
